package dao;

import java.util.ArrayList;
import java.util.List;

import domain.Roommate;

public class RoommateDAO extends GenericDAO<Roommate> {
	
	private static RoommateDAO instance;
	
	public static RoommateDAO getInstance() {
		if (instance == null) {
			instance = new RoommateDAO();
		}
		return instance;
	}

	@Override
	public Roommate store(Roommate entity) {
		if (entity.getId() != null) {
			return update(entity);
		}
		entity.setId(BalanceDAO.sequenceId++);
		entity.setVersion(0);
		this.entities.put(entity.getId(), entity.clone());
		return entity;
	}

	@Override
	public Roommate update(Roommate entity) {
		if (entity.getId() == null) {
			return store(entity);
		} else if (entity.getVersion().equals(find(entity.getId()).getVersion())) {
			return entity;
		}
		entity.incVersion();
		this.entities.put(entity.getId(), entity.clone());
		return entity;
	}
	
	@Override
	public List<Roommate> getAll() {
		List<Roommate> objects = new ArrayList<Roommate>();
		for (Roommate entity : entities.values()) {
			objects.add(entity.clone());
		}
		return objects;
	}
}
